package com.hebang.stcommunityapp.tools;

import com.alibaba.druid.support.json.JSONUtils;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class JsonUtil {

    public static <T> T getEntityByJsonString(String strJson, Class beanClass)
            throws Exception {
        Gson gson = new Gson();
        T t = (T) gson.fromJson(strJson, beanClass);

        return t;

    }

    public static String getJsonStringByEntity(Object o) {
        String strJson = "";
        Gson gson = new Gson();

        strJson = gson.toJson(o);

        return strJson;
    }

    public static <E> String getJsonStringByList(List<E> list) {
        StringBuilder strJson = new StringBuilder("[");
        Gson gson = new Gson();

        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1) {
                strJson.append(gson.toJson(list.get(i)) + ",");
            } else {
                strJson.append(gson.toJson(list.get(i)));
            }
        }

        strJson = strJson.append("]");

        return strJson.toString();
    }

    public static <T> List<T> getListByJsonString(String strJson,
                                                  Class<T> beanClass) throws JSONException {

        List<T> list = new ArrayList<T>();

        Gson gson = new Gson();
        JSONArray ja = new JSONArray(strJson);

        for (int i = 0; i < ja.length(); i++) {
            JSONObject jo = ja.getJSONObject(i);
            T t = gson.fromJson(jo.toString(), beanClass);
            list.add(t);
        }

        return list;

    }

    public static List<String> getListString(String strJson)
            throws JSONException {
        List<String> list = new ArrayList<String>();

        JSONArray ja = new JSONArray(strJson);
        for (int i = 0; i < ja.length(); i++) {
            list.add(ja.getString(i));
        }

        return list;
    }

    public static <T> List<List<List<T>>> get3ListByJsonString(String strJson,
                                                               Class<T> beanClass) throws JSONException {

        List<List<List<T>>> list = new ArrayList<List<List<T>>>();

        Gson gson = new Gson();
        JSONArray ja1 = new JSONArray(strJson);

        for (int i = 0; i < ja1.length(); i++) {
            JSONArray ja2 = ja1.getJSONArray(i);
            List<List<T>> list2 = new ArrayList<List<T>>();
            for (int j = 0; j < ja2.length(); j++) {
                JSONArray ja3 = ja2.getJSONArray(j);
                List<T> list3 = new ArrayList<T>();
                for (int k = 0; k < ja3.length(); k++) {
                    JSONObject jo = ja3.getJSONObject(k);
                    T t = gson.fromJson(jo.toString(), beanClass);
                    list3.add(t);
                }
                list2.add(list3);

            }
            list.add(list2);
        }

        return list;
    }

    public static <T> List<List<T>> get2ListByJsonString(String strJson,
                                                         Class<T> beanClass) throws JSONException {

        Gson gson = new Gson();
        JSONArray ja1 = new JSONArray(strJson);

        List<List<T>> list = new ArrayList<List<T>>();
        for (int j = 0; j < ja1.length(); j++) {
            JSONArray ja2 = ja1.getJSONArray(j);
            List<T> list2 = new ArrayList<T>();
            for (int k = 0; k < ja2.length(); k++) {
                JSONObject jo = ja2.getJSONObject(k);
                T t = gson.fromJson(jo.toString(), beanClass);
                list2.add(t);
            }
            list.add(list2);

        }

        return list;
    }

    public static <T> Map<Object, Object> getMapByJsonString(String strJson,
                                                             Class<T> beanClass) throws JSONException {
        Gson gson = new Gson();
        Map<Object, Object> map = null;

        JSONObject jo = new JSONObject(strJson);
        Iterator<String> it = jo.keys();

        while (it.hasNext()) {
            String key = (String) it.next();
            map.put(key, jo.get(key));
        }

        return map;

    }

    public static <T> Map<Object, Object> getMapByJsonString(String strJson) throws JSONException {
        Gson gson = new Gson();
        Map<Object, Object> map = null;

        JSONObject jo = new JSONObject(strJson);
        Iterator<String> it = jo.keys();

        while (it.hasNext()) {
            String key = (String) it.next();
            map.put(key, jo.get(key));
        }

        return map;

    }

    /**
     * 编写者任晋超
     * 通过response解析，直接返回解密后的message层数据。如果请求失败则返回false
     *
     * @param response
     * @return
     */
    public static String getMessage(Response response) {
        String  mess = "false";
        String resultCode = "";
        try {
            String resjson = response.body().string();
            Map result = (Map) JSONUtils.parse(resjson);
            if ((boolean) result.get("success")) { //获取数据成功
                mess = DESUtil.decrypt((String) result.get("message"));
//                mess =(String) result.get("message");`
//                mess = (String) result.get("message");
            } else {
//                mess = DESUtil.decrypt((String) result.get("message"));
//                mess =  result.get("message");
                Map mapmes = ((Map) getMapByJsonString(mess.toString()));
                mess = "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return mess;
    }

//    public static void main(String[] args) {
//        try {
////            String str = DESUtil.decrypt("HNlYKWxNbZxsGmwsDsmo+w==");
//            System.out.println("str:"+str);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }



}
